<?php

namespace app\api\controller;

use think\Db;
use think\Request;
use app\api\model\VolleyDesc as Balance;
use Vectorface\Whip\Whip;

class Task extends Common
{
    
      //音视频录制回调地址
    public function  transcribe(){


        echo  8888;
    }
    public function lists()
    {
        $where = ['is_hidden' => 0, 'channel'=>1];
        $where['sex_limit'] = $this->member['sex'] == '男' ? ['in', [0, 2]] : ['in', [0, 1]];
        $data = Db::name('task')->field('sort, is_hidden, sex_limit', 'filter')->where($where)->order('types asc, sort desc, id asc')->select();

        $finish = Db::name('user_task')->where(['user_id' => $this->member['id'],'channel'=>1])->column('task');

        $contain = [1 => [], 2 => []];
        $rewardType = ['金币', '积分'];
        foreach ($data as $item) {
            if ($item['name'] == 'sign_in') {
                $msg = explode(',', $item['reward']);
                $item['msg'] = '连签可得' . array_pop($msg) . $rewardType[$item['reward_type']];
            } else {
                $item['msg'] = '+' . $item['reward'] . $rewardType[$item['reward_type']];
            }

            if ($item['types'] == 1) {
                if (!in_array($item['name'], $finish)) {
                    $contain[1][] = $item;
                }
            } else {
                if ($item['name'] != 'sign_in') {
                    $count = Db::name('user_task')->where(['task' => $item['name'], 'user_id' => $this->member['id'],'channel'=>1])->whereTime('create_time', 'd')->count();
                    $count += 1;

                    $total = $count * $item['reward'];

                    if ($total <= $item['max']) {
                        $contain[2][] = $item;
                    }
                } else {
                    $result = Db::name('qiandao')->where(['uid' => $this->member['id'],'channel'=>1])->whereTime('time', 'd')->find();

                    $item['had_sign'] = $result ? 1 : 0;

                    $contain[2][] = $item;
                }
            }
        }

        $contain = array_values($contain);

        return $this->pass($contain);
    }

    // 每日签到
    public function qiandaoAjax()
    {
        $user_id = $this->member['id'];
        $data = $this->qiandaoTianshu($user_id);

        if (!$data) return $this->return['sign.error.exist'];

        if ($this->member['sex'] == '女') {
            Balance::beans($user_id, $data['money'], '连续签到 ' . $data['tianshu'] . ' 天', 1);
        } else {
            Balance::index($user_id, $data['money'], '连续签到 ' . $data['tianshu'] . ' 天', 1);
        }

        Db::name('qiandao')->insert(['channel'=>1,'uid' => $user_id, 'tianshu' => $data['tianshu'], 'time' => time(), 'ip' => (new Whip())->getValidIpAddress()]);

        return $this->pass('签到成功，获得' . $data['money'] . '金币');
    }

    // 根据签到的天数奖励
    private function qiandaoTianshu($uid)
    {
        $data = Db::name('qiandao')->master()->where(['uid' => $uid,'channel'=>1])->whereTime('time', 'today')->value('id');
        if ($data) return false;

        $data = Db::name('qiandao')->master()->where(['uid' => $uid,'channel'=>1])->whereTime('time', 'yesterday')->find();

        $reward = Db::name('task')->master()->where(['name' => 'sign_in','channel'=>1])->value('reward');
        $reward = explode(',', $reward);

        if ($data) {
            $times = count($reward);
            $day = $data['tianshu'] % $times;

            // 连续签到
            if ($day > $times) {
                $html['money'] = $reward[0];

                $tianshu = 1;

                $html['tianshu'] = $tianshu;
            } else {
                $html['money'] = $reward[$day];

                $html['tianshu'] = $data['tianshu'] + 1;
            }
        } else {
            $html['tianshu'] = 1;
            $html['money'] = $reward[0];
        }

        return $html;
    }

    // 签到记录
    public function qiandaorecords()
    {
        $user_id = $this->member['id'];

        $yesterday = strtotime(date('Ymd')) - 86400;

        $start = strtotime(date('Ym') . '01');
        $yesterday_record = Db::name('qiandao')->master()->where(['uid' => $user_id, 'time' => ['between', [$yesterday, $start]],'channel'=>1])->find();

        $where = ['uid' => $user_id,'channel'=>1];

        if ($yesterday_record) {
            if ($yesterday_record['tianshu'] != 1) {
                $where['tianshu'] = ['egt', 1];
            } else {
                $where['id'] = ['egt', $yesterday_record['id']];
            }
        } else {
            $where['time'] = ['egt', $start];
        }

        $qiandao = Db::name('qiandao')->master()->where($where)->order('id desc')->limit(7)->select();

        $status = false;
        foreach ($qiandao as $key => $item) {
            if ($item['tianshu'] == 1) {
                if ($status) {
                    unset($qiandao[$key]);
                } else {
                    $status = true;
                }
            } else if ($status) {
                unset($qiandao[$key]);
            }
        }

        $date = array_column($qiandao, 'id');
        array_multisort($date,SORT_ASC,$qiandao);


        $reward = Db::name('task')->where(['name' => 'sign_in','channel'=>1])->value('reward');
        $reward = explode(',', $reward);

        $data = [];
        foreach ($reward as $key => $item) {
            $is_sign = isset($qiandao[$key]) ? 1 : 0;

            $data[] = ['is_sign' => $is_sign, 'money' => $item];
        }

        if ($qiandao) {
            $count = count($qiandao);

            $isSign = $qiandao[$count - 1]['time'] >= strtotime(date('Ymd')) ? 1 : 0;
            $tianshu = $qiandao[$count - 1]['time'] >= $yesterday ? $qiandao[$count - 1]['tianshu'] : 0;
        } else {
            $isSign = 0;
            $tianshu = 0;
        }

        $data = [
            'data' => $data,
            'tianshu' => $tianshu,
            'is_sign' => $isSign,
        ];

        return $this->pass($data);
    }

    public function checkSignIn()
    {
        $result = Db::name('qiandao')->where(['uid' => $this->member['id'],'channel'=>1])->whereTime('time', 'd')->find();

        $signIn = $result ? 1 : 0;

        return $this->pass(['signIn' => $signIn]);
    }
    
    public function AppScore($images = '')
    {
        if (!$images) return $this->miss('请上传图片');

        $record = Db::name('task_for_app_score')->where(['user_id'=>$this->member['id'], 'status'=>['in',[0,1]]])->find();
        if ($record) return $this->miss($record['status'] ? '任务已完成' : '待审核');
        $res = Db::name('task_for_app_score')->insert([
            'user_id'   =>  $this->member['id'],
            'images'    =>  $images,
            'create_time'=>time()
        ]);
        return $res ? $this->pass('提交成功,待审核') : $this->miss('提交失败,请重试');
    }

    public function CheckAppScore(){
        $record = Db::name('task_for_app_score')->where(['user_id'=>$this->member['id'], 'status'=>['in',[0,1]]])->order('id desc')->find();
        $state = $record ? 0 : 1;
        if (!$record) {
            $hastask = Db::name('task')->where(['name'=>'app_score', 'reward'=>['>', 0], 'is_hidden'=>0])->count();
            $state = $hastask ? 1 : 0;
        }
        return $this->pass($state);
    }


    public function appdownload()
    {
        $list = Db::name('task_of_app_download')->order('sort desc')->field('create_time,update_time', 'filter')->select();
        $records = Db::name('task_of_app_download_records')->where(['user_id'=>$this->member['id']])->column('status', 'did');
        foreach ($list as &$value) {
            $value['status'] = 0;
            if (isset($records[$value['id']])) {
                $value['status'] = $records[$value['id']] + 1;
            }
        }
        return $this->pass($list);
    }
    public function Download($did, $images = '')
    {
        if (!$images) return $this->miss('请上传图片');

        $task = Db::name('task_of_app_download')->find($did);
        if (!$task) {
            return $this->miss('任务不存在');
        }

        $record = Db::name('task_of_app_download_records')->where(['user_id'=>$this->member['id'], 'did'=>$did, 'status'=>['in',[0,1]]])->find();
        if ($record) return $this->miss($record['status'] ? '任务已完成' : '待审核');
        $res = Db::name('task_of_app_download_records')->insert([
            'user_id'   =>  $this->member['id'],
            'did'       =>  $did,
            'images'    =>  $images,
            'create_time'=>time()
        ]);
        return $res ? $this->pass('提交成功,待审核') : $this->miss('提交失败,请重试');
    }
}
